package com.iteaj.iot.test.client.mutual;

import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.client.protocol.ServerInitiativeProtocol;
import com.iteaj.iot.test.IotTestProperties;
import com.iteaj.iot.utils.ByteUtil;

import java.nio.charset.StandardCharsets;

public class MutualClientTestProtocol extends ServerInitiativeProtocol<MutualClientMessage> {

    private IotTestProperties.MutualConnectProperties properties;

    public MutualClientTestProtocol(MutualClientMessage requestMessage) {
        super(requestMessage);
        properties = (IotTestProperties.MutualConnectProperties) getIotClient().getConfig();
    }

    @Override
    protected void doBuildRequestMessage(MutualClientMessage requestMessage) {
        byte[] message = requestMessage.getMessage();
        MutualType type = this.properties.getType();
        String msgStr = "";
        switch (type) {
            case HEX: msgStr = ByteUtil.bytesToHex(message); break;
            case UTF8: msgStr = new String(message, StandardCharsets.UTF_8); break;
            case ASCII: msgStr = new String(message, StandardCharsets.US_ASCII); break;
        }

        System.out.println("------------ 接收到服务端发送过来的["+type+"]报文：" + msgStr + " -------------------");
    }

    @Override
    protected MutualClientMessage doBuildResponseMessage() {
        byte[] message = this.requestMessage.getMessage();
        return new MutualClientMessage(message); // 再把接收到的报文返回
    }

    @Override
    public ProtocolType protocolType() {
        return null;
    }

}
